/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.variable.dtgenerator.api.mginterface.resinfo;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.MgrInterfaceGeneratorContext;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefValueObjResInfo;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class VarModelInfoGetResourceGenerator extends ClassMethodGenerator {

  private final String className;
  private final String currentSu;
  private final String metadataId;
  private final String rootNodeCode;
  private final String displayKey;
  private final CefApiGeneratorContext parentContext;

  public VarModelInfoGetResourceGenerator(String className, String currentSu, String metadataId,
      String rootNodeCode, String displayKey, CefApiGeneratorContext parentContext) {
    this.className = className;
    this.currentSu = currentSu;
    this.metadataId = metadataId;
    this.rootNodeCode = rootNodeCode;
    this.displayKey = displayKey;
    this.parentContext = parentContext;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    for (MediateType entityType : parentContext.getCefGenContext()
        .getEntityResourceInfos().values()) {
      String typeName = entityType.getFullName();
      TypeInfo type = new TypeInfo(typeName);
      type.setUseFullName(true);
      ClassInstanceCreation creation = ast.newClassInstanceCreation();
      creation.setType(type.getType(ast, parentContext));
      returnStatement.setExpression(creation);
    }

    block.statements().add(returnStatement);
    return block;
  }

  @Override
  protected String getMethodName() {
    return "getResourceInfo";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(CefValueObjResInfo.class);
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifiers() {
    ArrayList<ModifierKeyword> modifierKeywords = new ArrayList<>();
    modifierKeywords.add(ModifierKeyword.PROTECTED_KEYWORD);
    return modifierKeywords;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }
}
